import {searchPeriphery, keywordInput} from '../../utils/userLocation';
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        positionInfo: null, // 当前定位信息
        markerList: [], // marker列表

        nearbyList: [], // 附近列表
        nearByPage: 1, // 列表 页数
        keyword: null, // 地址关键字
    },

    // 搜索地址
    fnSearchAddress: function (e) {
        const {value} = e.detail;
        // console.log(value)
        this.setData({nearbyList: [], nearByPage: 1, keyword: value});
        if (value) { // 输入地址
            this.fnKeyWordInput(value, 1);
        } else { // 未输入地址=>搜索周边
            this.fnSearchPeriphery();
        }
        
    },

    // 搜索地址:关键字输入
    fnKeyWordInput: function (keyword, page) {
        let _that = this;
        // 城市名
        let cityName = this.data.positionInfo.address_component.city;
        keywordInput(keyword, page, cityName).then(function (res) {
            if (res.data.length > 0) {
                let list = [..._that.data.nearbyList, ...res.data];
                // console.log(list);
                _that.setData({nearbyList: list});
            } else {
                app.toast('没有更多了~');
            }
            
        })
    },

    // 地址列表滚动到底部
    fnBindscrolltolower: function() {
        // console.log('滚动到底部');
        const {nearByPage, keyword} = this.data;
        let page = nearByPage + 1;
        this.setData({nearByPage: page});
        if (keyword) {
            this.fnKeyWordInput(keyword, page);
        } else {
            this.fnSearchPeriphery(page);
        }
    },

    // 点击周边地址
    fnNearbyClick: function (e) {
        const {index} = e.currentTarget.dataset;
		let info = this.data.nearbyList[index];
        app.set('__address__info', info);
        console.log(info);
    },

    // 搜索周边
    fnSearchPeriphery: function (page) {
        let _that = this;
        let userAddress = app.get('__user__address');
        const {address, location} = userAddress;
        searchPeriphery(address, location.lat, location.lng, page).then(function (res) {
            // console.log('searchPeriphery', res);
            if (res.data.length > 0) {
                let list = [..._that.data.nearbyList, ...res.data];
                // console.log(list);
                _that.setData({nearbyList: list});
            } else {
                app.toast('没有更多了~');
            }
        })
        
    },

    // 地图初始化信息
    fnInitMapInfo: function () {
        let _that = this;
        let userAddress = app.get('__user__address');
        const {location} = userAddress;
        // marker信息
        let markerList = [];
        let marker = {
            id: 123,
            latitude: location.lat,
            longitude: location.lng,
            iconPath: '../../images/dw.png',
            width: 32,
            height: 32,
        };
        markerList.push(marker);
        _that.setData({positionInfo: userAddress, markerList});
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.fnInitMapInfo();

        // 搜索周边
        this.fnSearchPeriphery();

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})